package com.neu.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.neu.dao.impl.ProblemDao;
import com.neu.pojo.Model;
import com.neu.pojo.Problem;
import com.neu.pojo.Worker;
import com.neu.service.impl.ProblemService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

public class CreateProblemUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	
	private ProblemService problemService = ProblemService.getInstance();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CreateProblemUI frame = new CreateProblemUI(new Worker(),new Model());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CreateProblemUI(Worker w,Model m) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 530, 452);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("问题id");
		lblNewLabel.setBounds(96, 55, 58, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("题干");
		lblNewLabel_1.setBounds(96, 95, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("题目类型");
		lblNewLabel_2.setBounds(96, 140, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		JLabel lbla = new JLabel("选项A");
		lbla.setBounds(96, 184, 58, 15);
		contentPane.add(lbla);
		
		JLabel lblb = new JLabel("选项B");
		lblb.setBounds(96, 224, 58, 15);
		contentPane.add(lblb);
		
		JLabel lblc = new JLabel("选项C");
		lblc.setBounds(96, 266, 58, 15);
		contentPane.add(lblc);
		
		textField = new JTextField();
		textField.setBounds(222, 52, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(222, 92, 66, 21);
		contentPane.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(222, 137, 66, 21);
		contentPane.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(222, 181, 66, 21);
		contentPane.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(222, 221, 66, 21);
		contentPane.add(textField_4);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(222, 263, 66, 21);
		contentPane.add(textField_5);
		
		JButton btnNewButton = new JButton("确定");
		btnNewButton.setBounds(101, 348, 97, 23);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener((e)->{
			if(textField.getText().length() != 0&&textField_1.getText().length() != 0&&textField_2.getText().length() != 0
				&&textField_3.getText().length() != 0&&textField_4.getText().length() != 0&&textField_5.getText().length() != 0) {
				//输入正确， 新增问题
				Problem p = new Problem();
				p.setId(Integer.parseInt(textField.getText()));
				p.setProblem(textField_1.getText());
				p.setType(textField_2.getText());
				p.setOptionA(textField_3.getText());
				p.setOptionB(textField_4.getText());
				p.setOptionC(textField_5.getText());
				
				if(problemService.getInstance().add(p)) {//存入文件
					JOptionPane.showMessageDialog(null, "添加成功");//提示
				}else {
					JOptionPane.showMessageDialog(null, "添加失败");//提示
				}
				AddProblemUI addProblemUI = new AddProblemUI(w, m);//跳转新窗口
				addProblemUI.setVisible(true);
				setVisible(false);
			}else {
				JOptionPane.showMessageDialog(null, "请输入内容");//提示
			}
		});
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.setBounds(278, 346, 97, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener((e)->{
			AddProblemUI addProblemUI = new AddProblemUI(w, m);//跳转新窗口
			addProblemUI.setVisible(true);
			setVisible(false);
		});
		
		//窗口居中
		setLocationRelativeTo(null);
	}
}
